//
//  MainTabbarViewController.swift
//  CXSwiftWB
//
//  Created by CaoXuan on 2017/5/7.
//  Copyright © 2017年 meidaer. All rights reserved.
//

import UIKit

class MainTabbarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        tabBar.tintColor = UIColor.orange
//        addChildViewController(HomeTableViewController())
//        addChildViewController(nil, title: nil, imageName: nil)
//        addChildViewControllers()
        tabBar.addSubview(composeButton)
        // 保存按钮尺寸
        let rect = composeButton.frame
        // 计算宽度
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        // 设置按钮的位置
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
    }
    
    // MARK: - 内部控制方法
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(composeButton)
        // 保存按钮尺寸
        let rect = composeButton.frame
        // 计算宽度
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        // 设置按钮的位置
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
    }
    
    /// 添加所有的子控制器
    private func addChildViewControllers() {
        guard let filePath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
            return
        }
        
        guard let date =  NSData(contentsOfFile: filePath) else {
            return
        }
        do
        {
         let objc = try JSONSerialization.jsonObject(with: date as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String: AnyObject]]
            
            for dict in objc {
                let name = dict["title"] as? String
                let vcName = dict["vcName"] as? String
                let imageName = dict["imageName"] as? String
                addChildViewController(vcName, title: name, imageName: imageName)
            }
        } catch {
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("NullViewController", title: "", imageName: "")
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
    }
    
    private func addChildViewController(_ childController: String?, title: String?, imageName: String?) {
        guard  let name =  Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            return
        }
        
        var cls: AnyClass? = nil
        if let vcName = childController {
            cls = NSClassFromString(name + "." + vcName)
        }
        
        guard let typeCls = cls as? UIViewController.Type else {
            return
        }
        
        let childVC = typeCls.init()
        
        // 设置控制器的相关属性
        childVC.title = title
        if let ivName = imageName {
            childVC.tabBarItem.image = UIImage(named: ivName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
            childVC.tabBarItem.selectedImage = UIImage(named: ivName + "_highlighted")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        }
        
        let nav = UINavigationController(rootViewController: childVC)
        addChildViewController(nav)
    }
    
    // MARK: - 按钮点击
    @objc private func compseBtnClick(_ btn: UIButton)
    {
        
    }
    
    private lazy var composeButton: UIButton = {
        () -> UIButton
        in
        let btn = UIButton(imageName:"tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
        
        btn.addTarget(self, action: #selector(compseBtnClick(_:)), for: UIControlEvents.touchUpInside)
        return btn
    }()
}
